package com.hdax.dmw.order.listener;

import cn.hutool.core.util.ObjectUtil;
import com.hdax.dmw.entity.order.TxMsg;
import com.hdax.dmw.order.service.TxMsgService;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class OrderConfirmCallback implements RabbitTemplate.ConfirmCallback {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    //在当前类型对象创建成功并加入SPring容器之后执行
    @PostConstruct
    public void init(){
        rabbitTemplate.setConfirmCallback(this);
    }

    @Autowired
    private TxMsgService txMsgService;
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        if(correlationData!=null){
            String orderNo = correlationData.getId();
            //查询本地消息表
            TxMsg txMsg = txMsgService.getById(orderNo);
            if(ObjectUtil.isNotEmpty(txMsg)){
                //修改消息表，确认投递消息成功
                txMsg.setMsgResult(1);
                txMsg.setVersion(txMsg.getVersion()+1);
                txMsgService.updateById(txMsg);
            }
        }
    }
}
